
module windump.xml.element;

import windump.xml.node;


class XmlElement : XmlNode
{
    protected char[][char[]]  m_attributes;

    public this(char[] name)
    {
        super(name);
    }

    protected char[] getAttributesText()
    {
        char[] ret;        
        foreach(char[] name; m_attributes.keys)
        {
            char[] value = m_attributes[name];
            ret ~= name ~ `="` ~ XmlEntities.encode(value) ~ `" `;
        }
        if(ret.length > 0)ret = ret[0..length - 1];
        return ret;
    }

    public char[] outerXml()
    {
        char[] ret = "<" ~ name;
        if(m_attributes.length > 0) ret ~= ' ';
        ret ~= getAttributesText();        

        if(!hasChildren)
            return ret ~ " />\n";
        else
        {
            if(m_attributes.length > 0)ret ~= " >\n";
            else ret ~= ">\n";
        }

        foreach(XmlNode child; m_children)
        {
            ret ~= child.outerXml;
        }

        ret ~= "</" ~ name ~ ">\n";

        return ret;
    }

    public override char[] innerXml()
    {
        return "";
    }

    public void setAttribute(char[] name, char[] value)
    {
        m_attributes[name] = value;
    }

    public char[] getAttribute(char[] name)
    {
        return m_attributes[name];
    }

}
